<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2016/8/30
 * Time: 10:38
 */

use yii\helpers\Url;
$this->title = '卡片模板管理';
?>

<script type="application/javascript">
    $(function ($) {
        $('#myModalEdit').modal({
            keyboard: false, backdrop: 'static', show: false
        });
        $('#myModalDetail').modal({
            keyboard: false, backdrop: 'static', show: false
        });
    });

    function filter() {
        var card_tpl_name = $("#card_tpl_name").val();

        var href = window.location.href;
        var index = href.indexOf("?page=");
        if (index > 0) {
            href = href.substr(0, index);
        }
        var href_str = href + "?page=1";


        if (card_tpl_name != '' && card_tpl_name != null) {
            href_str = href_str + "&name=" + card_tpl_name;
        }

        window.location.href = href_str;
    }

    function onAddData(id) {
        $("#myModalEdit").modal('show');
        var data = {};
        if (id != undefined) {
            data.id = id;
        }
        modalEdit(data);
    }

    //打开新增修改页面
    function modalEdit(data) {
        $("#modaledit").html('');
        requestHtml('card-tpl-part-view', data, true, function (responseHtml) {
            $("#modaledit").html(responseHtml);
        });
    }

    //新增/修改
    function saveData() {
        var id = $("#modaledit_submit_form").find("#id").val();
        var name = $("#modaledit_submit_form").find("#name").val();
        var description = $("#modaledit_submit_form").find("#description").val();
        var integral = $("#modaledit_submit_form").find("#integral").val();
        var page_image_url = $("#modaledit_submit_form").find("#page_image_url").val();


        var goods = {};
        goods.list = [];
        var goodsChecks = $("#goods_table").find('input:checkbox[name=goods]:checked');
        for (var i = 0; i < goodsChecks.length; i++) {
            var g = {};
            g.code = $(goodsChecks[i]).val();
            g.sort = $(goodsChecks[i]).parent().prev().find('input').val();
            goods.list.push(g);
        }

        $("#card_tpl_goods").val(JSON.stringify(goods));
        if (name == '' || name == null) {
            $("#hint_message").text('卡片名称不能为空！');
            $("#hintMessageModal").modal('show');
            return false;
        }

        if (integral == '' || integral == null) {
            $("#hint_message").text('价值积分不能为空！');
            $("#hintMessageModal").modal('show');
            return false;
        }

        if (goods.list.length <= 0) {
            $("#hint_message").text('请选择卡片模板产品！');
            $("#hintMessageModal").modal('show');
            return false;
        }
        //Form提交
        loading.show();
        $("#modaledit_submit_form").submit();
        $("#upload_exec_target").load(function () {
            try {
                var result = eval('(' + ($("#upload_exec_target").contents().find("body").html()) + ')');
                if (result.status == 0) {
                    filter();
                } else {
                    var re = result.message;
                    $("#hint_message").html(re);
                }
            } catch (err) {
                var re = "系统异常，请重试";
                $("#hint_message").html(re);
            }
            loading.hide();
        });
    }

    //删除数据
    function delData() {
        var id = $("#deleteDataId").val();
        delClose();
        if (id != null && id != '') {
            var data = {};
            data.id = id;
            requestJson("card-tpl-del", data, true, function (response) {
                if (response.status == 0) {
                    $("#hint_message").text('操作成功！');
                    window.location.reload();
                } else {
                    $("#hint_message").text('操作失败！');
                }
                $("#hintMessageModal").modal('show');
            });
        } else {
            $("#hint_message").text('需删除的数据有误，请重试！');
            $("#hintMessageModal").modal('show');
        }
    }


    //删除按键点击事件
    function delClick(id) {
        $("#deleteDataId").val(id);
    }

    //删除取消
    function delClose() {
        $("#deleteDataId").val('');
        $("#deleteModal").modal('hide');
    }

    function getDetail(id) {
        $("#myModalDetail").modal('show');
        var data = {};
        data.id = id;
        $("#modalDetail").html('');

        requestHtml('card-tpl-part-detail', data, true, function (responseHtml) {
            $("#modalDetail").html(responseHtml);
        });
    }
</script>
<style type="text/css">
    .modal-body {
        overflow: auto;
        max-height: 600px;
    }
</style>

<!-- head filter-->
<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="btn-group">
                <button class="btn btn-primary" type="button" onclick="onAddData()"><i class="fa fa-plus"></i> 新增
                </button>
            </div>
            <div class="pull-right">
                <input type="text" class="form-control" id="card_tpl_name" placeholder="模板名称" value="<?= $name ?>">
                <button type="button" class="btn btn-primary" onclick="filter()"><i class="fa fa-search"></i> 搜索
                </button>
            </div>
        </div>
    </div>
</div>
<!-- body table-->
<div class="panel panel-default" id="card_tpl_list">
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed">
                <thead class="nowrap">
                <tr>
                    <th style="width:20%">卡片模板名称</th>
                    <th style="width:20%">价值积分</th>
                    <th style="width:15%">描述</th>
                    <th style="width:15%">创建时间</th>
                    <th style="width:15%">创建人</th>
                    <th style="width:15%">操作</th>
                </tr>
                </thead>
                <tbody>
                <?php
                if ($list != null && $list != '') {
                    for ($i = 0; $i < count($list); $i++) {
                        ?>
                        <tr>
                            <td>
                                <button class="btn btn-link btn-xs" type="button"
                                        onclick="getDetail('<?= $list[$i]['id'] ?>')"
                                        data-toggle="modal"
                                        data-target="#myModalDetail">
                                    <?= $list[$i]['name'] ?>
                                </button>
                            </td>
                            <td>
                                <?= $list[$i]['integral'] ?>
                            </td>
                            <td>
                                <?= $list[$i]['description'] ?>
                            </td>
                            <td>
                                <?= $list[$i]['create_datetime'] ?>
                            </td>
                            <td>
                                <?= $list[$i]['create_manager_name'] ?>
                            </td>
                            <td>
                                <div class="btn-group">
                                    <button class="btn btn-link btn-xs" type="button"
                                            onclick="onAddData('<?= $list[$i]['id'] ?>')"
                                            data-toggle="modal"
                                            data-target="#myModalEdit">
                                        修改
                                    </button>
                                    <button class="btn btn-link btn-xs" type="button"
                                            data-toggle="modal"
                                            data-target="#deleteModal"
                                            onclick="delClick('<?= $list[$i]['id'] ?>')">
                                        删除
                                    </button>
                                </div>
                            </td>
                        </tr>
                        <?php
                    }
                }
                ?>
                </tbody>
            </table>
        </div>
        <div class="pull-left">
            <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
        </div>
    </div>
</div>

<!-- Modal card Edit-->
<div class="modal fade" id="myModalEdit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">卡片模板</h4>
            </div>
            <div id="modaledit" class="modal-body">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="saveData()"><i class="fa fa-save"></i> 保存
                </button>
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </button>
            </div>
        </div>
    </div>
</div>

<!-- Modal card detail-->
<div class="modal fade" id="myModalDetail" tabindex="-1" role="dialog" aria-labelledby="myModalDetailLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalDetailLabel">模板详情</h4>
            </div>
            <div id="modalDetail" class="modal-body">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </button>
            </div>
        </div>
    </div>
</div>
<!--删除确认对话框-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="deleteModal" class="modal fade">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">是否确认操作？</h4>
                <input type="hidden" id="deleteDataId" class="form-control" value="">
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" onclick="delData()"><i class="fa fa-ok"></i> 确认</button>
                <button class="btn btn-primary" onclick="delClose()"><i class="fa fa-remove"></i> 取消</button>
            </div>
        </div>
    </div>
</div>

<!--提示对话框-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="hintMessageModal" class="modal fade">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">提示</h4>
            </div>
            <div class="modal-body" id="hint_message" align="center">
            </div>
        </div>
    </div>
</div>